package com.gdpu.service.impl;

import com.gdpu.api.CommonPage;
import com.gdpu.config.RedisConfig;
import com.gdpu.dao.PmsGoodDao;
import com.gdpu.mapper.PmsGoodsMapper;
import com.gdpu.model.PmsGoods;
import com.gdpu.model.PmsGoodsExample;
import com.gdpu.service.PortalPmsGoodsService;
import com.gdpu.vo.PmsGoodDetailVo;
import com.gdpu.vo.PmsGoodsByCateVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.security.spec.ECField;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:weiming
 * @Date:2021/6/11
 */
@Service
public class PortalPmsGoodsServiceImpl implements PortalPmsGoodsService {

    @Autowired
    private PmsGoodDao pmsGoodDao;
    @Autowired
    private PmsGoodsMapper pmsGoodsMapper;


    @Override
    public CommonPage listByCategoryId(Integer categoryId, Integer pageNum, Integer pageSize) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        PmsGoodsExample example = new PmsGoodsExample();
        example.setOrderByClause("sort desc");
        example.createCriteria().andCategoryIdEqualTo(categoryId)
                .andIsDeletedEqualTo(0).andIsSaleEqualTo(1);
        List<PmsGoods> pmsGoods = pmsGoodsMapper.selectByExample(example);
        List<PmsGoodsByCateVo> pmsGoodsByCateVoList = new ArrayList<>(pmsGoods.size());
        for (PmsGoods pmsGood : pmsGoods) {
            PmsGoodsByCateVo pmsGoodsByCateVo = new PmsGoodsByCateVo();
            pmsGoodsByCateVo.setGoodName(pmsGood.getGoodName());
            pmsGoodsByCateVo.setGoodPrice(pmsGood.getGoodPrice());
            pmsGoodsByCateVo.setId(pmsGood.getId());
            pmsGoodsByCateVo.setMainImage(pmsGood.getMainImage());
            pmsGoodsByCateVo.setSaleNum(pmsGood.getSaleNum());
            pmsGoodsByCateVoList.add(pmsGoodsByCateVo);
        }
        return CommonPage.restPage(pmsGoodsByCateVoList, page);
    }

    @Override
    @Cacheable(value = RedisConfig.REDIS_KEY_DATABASE,key = "'pms:good:'+#goodId")
    public PmsGoodDetailVo detail(Integer goodId) {
        PmsGoods pmsGoods = pmsGoodsMapper.selectByPrimaryKey(goodId);
        if(pmsGoods.getIsSale()==0){

        }
        PmsGoodDetailVo pmsGoodDetailVo = new PmsGoodDetailVo();
        BeanUtils.copyProperties(pmsGoods,pmsGoodDetailVo);
        return pmsGoodDetailVo;
    }
}
